package com.qf.usermybatis.controller;

import com.github.pagehelper.PageInfo;
import com.qf.usermybatis.entity.User;
import com.qf.usermybatis.service.UserService;
import com.qf.usermybatis.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @author nk
 * @date 2022/4/24 19:17
 */
@WebServlet("/users")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        try{
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            if (Objects.isNull(pageNum) || "".equals(pageNum)){
                pageNum="1";
            }
            if (Objects.isNull(pageSize) || "".equals(pageSize)){
                pageSize="5";
            }
            PageInfo<User> pageInfo = userService.selectAll(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
            req.setAttribute("pageInfo",pageInfo);
            req.getRequestDispatcher("index.jsp").forward(req,resp);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
